package com.snail.snailvr.views.fragments;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.snail.snailvr.R;
import com.snail.snailvr.interfaces.WebViewInterface;
import com.snail.snailvr.utils.chromeClient;

import butterknife.Bind;

/**
 * Created by snail on 16/4/14.
 */
public class WebViewFragment extends BaseFragment{
    @Bind(R.id.web_view)
    WebView mWebView;
    @Bind(R.id.webview_progress_bar)
    ProgressBar mProgressBar;
    @Override
    protected void handleIntent() {

    }

    @Override
    protected void initView() {
        setupWebView();
    }
    private void setupWebView() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        String appCachePath = getActivity().getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        // 支持缩放，在SDK11以上，不显示缩放按钮
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        if (Build.VERSION.SDK_INT >= 11) {
            webSettings.setDisplayZoomControls(false);
        }
        // 自适应网页宽度
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        mWebView.addJavascriptInterface(new WebViewInterface(mWebView,getActivity()), WebViewInterface.class.getSimpleName());
        mWebView.setWebChromeClient(new chromeClient(mProgressBar));
        mWebView.setWebViewClient(new CommonWebViewClient());
    }

    public class CommonWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("tel:")) {
                if (isAdded()) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                    startActivity(intent);
                }
            } else {
                view.loadUrl(url);
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            hideWebLoadingUI();
        }


        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            return super.shouldInterceptRequest(view, url);
        }
    }

    public void hideWebLoadingUI() {
        if (mProgressBar != null)
            mProgressBar.setVisibility(View.GONE);
    }
    @Override
    protected void loadData() {
//        mWebView.loadUrl("http://www.snail.com");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        loadWhenUserVisible(isVisibleToUser);
    }

    protected void loadWhenUserVisible(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (!this.isFragmentVisible) {
               loadData();
                this.isFragmentVisible =true;
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_webview;
    }
}
